package com.sander.guardmanagement.ui.activity;

import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.UserManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.sander.guardmanagement.R;
import com.sander.guardmanagement.http.BaseCallback;
import com.sander.guardmanagement.http.HomeSaveUserHomeinfo;
import com.sander.guardmanagement.http.OkHttpHelper;
import com.sander.guardmanagement.response.ResponseModel;
import com.sander.guardmanagement.ui.model.SelectedBoxModel;
import com.sander.guardmanagement.until.UserInfoManager;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

public class SupplementaryInformationActivity extends AppCompatActivity {

    SelectedBoxModel boxModel;

    Button yezhubutton;
    Button zukebutton;

    EditText name_inputView;
    EditText age_inputView;

    public int selected_index;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_supplementary_information);
        Intent intent = getIntent();
        boxModel = (SelectedBoxModel) intent.getSerializableExtra("box");
        TextView textView = (TextView) findViewById(R.id.actiontitlellabel);
        textView.setText(boxModel.getBoxTitle());

        initData();
        initView();
        initCallBack();

        this.selected_index = 0;
        onselectedIndex(0);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);


    }

    private void initCallBack(){
        FrameLayout frameLayout = (FrameLayout) findViewById(R.id.callbackLayout);
        frameLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    public void initData()
    {

    }
    public void initView()
    {
        name_inputView = (EditText) findViewById(R.id.inputnametextview);
        age_inputView = (EditText) findViewById(R.id.inputagetextview);

        Button surebutton = (Button) findViewById(R.id.sureqpplycooperation);
        surebutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onsureButton();

            }
        });

        yezhubutton = (Button) findViewById(R.id.yezhubutton);
        zukebutton = (Button)findViewById(R.id.zukebutton);
        yezhubutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onselectedIndex(0);
            }
        });

        zukebutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onselectedIndex(1);

            }
        });

    }

    public void onsureButton(){
        String name_string = name_inputView.getText().toString();

        String age_string = age_inputView.getText().toString();
        if (TextUtils.isEmpty(name_string)){
            showToastMessage("姓名不能为空");
            return;
        }
        if (TextUtils.isEmpty(age_string)){
            showToastMessage("年龄不能为空");
            return;
        }

        HomeSaveUserHomeinfo saveUserHomeinfo = new HomeSaveUserHomeinfo();
        saveUserHomeinfo.uid = UserInfoManager.sharedManager().getUserInfo().getId() + "";
        saveUserHomeinfo.city_id = boxModel.city_id;
        saveUserHomeinfo.community_id = boxModel.community_id;
        saveUserHomeinfo.sub_community_id = boxModel.sub_community_id;
        saveUserHomeinfo.building_id = boxModel.building_id;
        saveUserHomeinfo.room_id = boxModel.room_id;
        saveUserHomeinfo.user_name = name_string;
        saveUserHomeinfo.user_age = age_string;
        saveUserHomeinfo.user_type = (this.selected_index + 1) + "";

        OkHttpHelper.getinstance().post(saveUserHomeinfo.requestUrl(), saveUserHomeinfo.getValueOfClas(), new BaseCallback<ResponseModel>() {
            @Override
            public void onRequestBefore() {

            }

            @Override
            public void onFailure(Request request, Exception e) {

            }

            @Override
            public void onSuccess(Response response, ResponseModel o) {
                Log.d("sander","--> " + o.getMsg());
                if (o.getCode() == 0){
                    Intent intent  = new Intent(SupplementaryInformationActivity.this,MainActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }else
                {
                    showToastMessage(o.getMsg());
                }

            }

            @Override
            public void onError(Response response, int errorCode, Exception e) {

            }
        });


    }


    public void onselectedIndex(int index){
        this.selected_index = index;
        Resources res = getResources( );
        Drawable shape = res.getDrawable(R.drawable.shape);
        Drawable grayshape = res.getDrawable(R.drawable.grayshape);
        if (index == 0){
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                yezhubutton.setBackground(getResources().getDrawable(R.drawable.shape,null));
                zukebutton.setBackground(getResources().getDrawable(R.drawable.grayshape,null));
            }else{
                yezhubutton.setBackground(shape);
                zukebutton.setBackground(grayshape);
            }
        }else if (index == 1){
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                yezhubutton.setBackground(getResources().getDrawable(R.drawable.grayshape,null));
                zukebutton.setBackground(getResources().getDrawable(R.drawable.shape,null));
            }else{
                yezhubutton.setBackground(grayshape);
                zukebutton.setBackground(shape);
            }
        }
    }

    public void showToastMessage(String message){
        Toast.makeText(getApplicationContext(),message,Toast.LENGTH_SHORT).show();

    }
}
